package com.source.baseData.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.baseData.entity.SlaughterHouse;
import com.source.baseData.mapper.SlaughterHouseMapper;
import com.source.baseData.service.SlaughterHouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.source.baseData.vo.SlaughterHouseAddressVo;
import com.source.baseData.vo.SlaughterHouseMapVo;
import com.source.labelAndBarcode.vo.SlaughterHouseVo;
import com.source.utils.ImageCatalogueEnum;
import com.source.utils.ImageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Joey
 * @since 2021-02-10
 */
@Service
public class SlaughterHouseServiceImpl extends ServiceImpl<SlaughterHouseMapper, SlaughterHouse> implements SlaughterHouseService {

    @Resource
    private SlaughterHouseMapper houseMapper;

    @Override
    public List<SlaughterHouseVo> findAllVo() {
        return houseMapper.findAllVo();
    }

    @Override
    public List<SlaughterHouseVo> findAllOnPermission() {
        return houseMapper.findAllOnPermission();
    }

    @Override
    public List<SlaughterHouseVo> findAllOnPermissionAudit() {
        return houseMapper.findAllOnPermissionAudit();
    }

    @Override
    public List<SlaughterHouseVo> findAllOnDeletionAudit() {
        return houseMapper.findAllOnDeletionAudit();
    }

    @Override
    public SlaughterHouseVo findVoById(String id) {
        return houseMapper.findVoById(id);
    }

    @Override
    public IPage<SlaughterHouseVo> findAllVoOnPaginationAndPermission(Page<SlaughterHouseVo> page) {
        return houseMapper.findAllVoOnPaginationAndPermission(page);
    }

    @Override
    public IPage<SlaughterHouseVo> findAlikeOnPAP(Page<SlaughterHouseVo> page, String keyWord) {
        return houseMapper.findAlikeOnPAP(page, "%"+keyWord+"%");
    }

    @Override
    public IPage<SlaughterHouseVo> findVoByNameAndP(Page<SlaughterHouseVo> page, String keyWord) {
        return houseMapper.findVoByNameAndP(page,"%"+keyWord+"%");
    }

    @Override
    public IPage<SlaughterHouseVo> findAllOnMultiStatus(Page<SlaughterHouseVo> page, int status) {
        return houseMapper.findAllOnMultiStatus(page,status);
    }

    @Override
    public IPage<SlaughterHouseVo> findAllOnPagination(Page<SlaughterHouseVo> page) {
        return houseMapper.findAllOnPagination(page);
    }

    @Override
    public int saveImage(SlaughterHouse sh, MultipartFile image, HttpServletRequest request) throws Exception {
        if(image != null){
            sh.setStatus(2);
            String s = ImageUtils.saveImage(image, ImageCatalogueEnum.SlaughterHouse_Catalogue);

            String mag = sh.getMarketPictures();
            if(StringUtils.isEmpty(mag)){

            }{
                ImageUtils.delete(mag);
            }
            sh.setMarketPictures(s);
            return houseMapper.insert(sh);
        }
        return 0;
    }

    @Override
    public IPage<SlaughterHouseVo> findByUnChecked(Page<SlaughterHouseVo> page) {
        return houseMapper.findByUnChecked(page);
    }

    @Override
    public List<SlaughterHouseVo> findByDistrict(String name) {
        return houseMapper.findByDistrict(name);
    }

    @Override
    public IPage<SlaughterHouseVo> findByUnCheckedAndKeyWord(String keyWord, Page<SlaughterHouseVo> page) {
        return houseMapper.findByUnCheckedAndKeyWord("%"+keyWord+"%",page);
    }

    @Override
    public List<SlaughterHouseMapVo> getAllSlaughterHouseMapVo() {
        return houseMapper.selectAllSlaughterHouseMapVo();
    }

    @Override
    public List<SlaughterHouseAddressVo> selectSlaughterHouseAddressVo() {
        return houseMapper.selectSlaughterHouseAddressVo();
    }
}
